package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.BankDAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.BankDataList;
import com.mbc.rmt.entities.Bank;
import com.mbc.rmt.entities.BankRpt;


public class BankMgr extends AbstractMgr {
	
	private BankDAO bankDAO;
	private LogDAO logDAO;
	
	public BankMgr(){}
	
	public BankMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public void create(Bank entity) throws Exception {
		openConnection();
		try {
			bankDAO = RemittanceDAOFactory.getBankDAO(dbManager.getConnection());
			entity.setBankId(KeyGenerator.generateKey(getConnection()));
			bankDAO.create(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity =  new Log();
			l_logentity.setTablename("Bank");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getBankId());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
		} catch (SQLException e) {
			if(DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void update(Bank entity) throws Exception {
		openConnection();
		try {
			bankDAO = RemittanceDAOFactory.getBankDAO(dbManager.getConnection());
			bankDAO.update(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("Bank");
			l_logentity.setKey(entity.getBankId());
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void delete(long key, long userid) throws Exception {
		openConnection();
		try {
			bankDAO = RemittanceDAOFactory.getBankDAO(dbManager.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			Log l_logentity = new Log();
			l_logentity.setTablename("Bank");
			l_logentity.setKey(key);
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			bankDAO.delete(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public Bank find(long key) throws Exception {
		openConnection();
		try {
			bankDAO = RemittanceDAOFactory.getBankDAO(dbManager.getConnection());
			return bankDAO.find(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public Bank findByCode(String code, long branchId)throws Exception{
		openConnection();
		try {
			bankDAO = RemittanceDAOFactory.getBankDAO(dbManager.getConnection());
			return bankDAO.findByCode(code, branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public List<Bank> findAll() throws Exception {
		openConnection();
		try {
			bankDAO = RemittanceDAOFactory.getBankDAO(dbManager.getConnection());
			return bankDAO.findAll();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public BankDataList findBanks(Bank cri)throws Exception{
		openConnection();
		try {
			bankDAO = RemittanceDAOFactory.getBankDAO(dbManager.getConnection());
			return bankDAO.findBanks(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	//MBC Begin
	/*public BankDataList getBankList(BankRpt entity)throws Exception{
		openConnection();
		try {
			bankDAO = RemittanceDAOFactory.getBankDAO(dbManager.getConnection());
			return bankDAO.getBankList(entity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}*/
	
	public JasperPrint prepareBankReport(BankRpt entity) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			bankDAO = RemittanceDAOFactory.getBankDAO(dbManager.getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(bankDAO.prepareBankReport(entity));
			 JasperReport l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/Bank.jasper");
			 HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
	//MBC End

	public Bank getBank(Bank l_info) throws Exception {
		openConnection();
		try{
			bankDAO = RemittanceDAOFactory.getBankDAO(dbManager
					.getConnection());
			String code=l_info.getBankCode();
			long branchID =  l_info.getBranchId();
			return bankDAO.findByCode(code,branchID);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
}
